/*
 * Copyright (c) JoinQuant Development Team
 * Author: Huayong Kuang <kuanghuayong@joinquant.com>
 */

#ifndef MY_DATETIME_H
#define MY_DATETIME_H

#include <iostream>
#include <ctime>
#include <sys/time.h>

using namespace std;

class DateTime {
public:
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
    int microsecond;

    DateTime();
    DateTime(int year, int month, int day,
             int hour=0, int minute = 0, int second = 0, int microsecond = 0);
    DateTime(struct timeval tv);
    DateTime(struct tm time);
    ~DateTime();

    // 获取当前时间
    static DateTime now();
    // 从微秒时间戳创建时间
    static DateTime fromtimestamp(int64_t usec);
    static DateTime fromstr(const string &str, const string &format, bool hasUsec=false);

    // 转化为字符串
    string tostr(const string &format="%F %T", int usecMode=2);

private:
    struct timeval m_tv;
    struct tm      m_tm;

    void setValues();
};

#endif
